/**
 * Copyright 2015 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2015年10月5日 上午11:04:18
 */
package com.absir.appserv.system.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.absir.appserv.configure.JConfigureUtils;
import com.absir.appserv.system.bean.JPayTrade;
import com.absir.appserv.system.bean.proxy.JiUserBase;
import com.absir.appserv.system.configure.JVivoConfigure;
import com.absir.appserv.system.helper.HelperClient;
import com.absir.appserv.system.helper.HelperJson;
import com.absir.appserv.vivo.VivoSignUtils;
import com.absir.bean.inject.value.Bean;
import com.absir.core.kernel.KernelString;

/**
 * @author absir
 *
 */
@Bean
public class VivoService implements IdentityService, IPayInterface, IPayOrder {

	/** VIVO_CONFIGURE */
	public static final JVivoConfigure VIVO_CONFIGURE = JConfigureUtils.getConfigure(JVivoConfigure.class);

	/** PLAT_FORM_NAME */
	public static final String PLAT_FORM_NAME = "Vivo";

	/** queryLoginUrl */
	private String queryLoginUrl = "https://usrsys.inner.bbk.com/auth/user/info";

	/** orderTradeUrl */
	private String orderTradeUrl = "https://pay.vivo.com.cn/vcoin/trade";

	/**
	 * @author absir
	 *
	 */
	public static class LoginData {

		public String uid;

		public String mail;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.absir.appserv.system.service.IdentityService#getUserBase(java.lang
	 * .String[])
	 */
	@Override
	public JiUserBase getUserBase(String[] parameters) {
		if (parameters.length == 2) {
			LoginData loginData = HelperClient.openConnection(queryLoginUrl + "?access_token=" + parameters[1], false, null,
					LoginData.class);
			if (loginData != null && loginData.uid != null) {
				return SecurityService.ME.openUserBase(loginData.uid, null, PLAT_FORM_NAME);
			}
		}

		return null;
	}

	/**
	 * @author absir
	 *
	 */
	public static class OrderData {

		// 响应码 成功返回：200，非200时，respMsg会提示错误信息。
		public int respCode;

		// 响应信息 对应响应码的响应信息
		public String respMsg;

		// 当respCode=200时，下列参数有效

		// 签名方法 对关键信息进行签名的算法名称：MD5
		public String signMethod;

		// 签名信息 对关键信息签名后得到的字符串，用于商户验签，签名规则请参考签名计算说明
		public String signature;

		// vivoSDK需要的参数 vivoSDK使用
		public String accessKey;

		// 交易流水号 vivo订单号
		public String orderNumber;

		// 交易金额 单位：分，币种：人民币，必须是整数
		public int orderAmount;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.absir.appserv.system.service.IPayInterface#validator(com.absir.appserv
	 * .system.bean.JPayTrade)
	 */
	@Override
	public boolean validator(JPayTrade payTrade) throws Exception {
		return false;
	}

	public static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.absir.appserv.system.service.IPayOrder#order(com.absir.appserv.system
	 * .bean.JPayTrade, java.util.Map)
	 */
	@Override
	public Object order(JPayTrade payTrade, Map<String, Object> paramMap) throws Exception {
		Map<String, String> params = HelperJson.buildParams(paramMap);
		params.put("cpId", VIVO_CONFIGURE.getPartnerId());
		params.put("appId", VIVO_CONFIGURE.getAppId());
		params.put("cpOrderNumber", payTrade.getId());
		params.put("orderAmount", String.valueOf(((int) payTrade.getAmount() * 100)));
		params.put("orderTime", DATE_FORMAT.format(new Date()));
		params.put("extInfo", "");
		params.put("signMethod", "MD5");
		params.put("signature", VivoSignUtils.getVivoSign(params, VIVO_CONFIGURE.getMerchantKey()));
		OrderData orderData = HelperClient.openConnection(orderTradeUrl + "?" + KernelString.implode(params, '&', '='), null,
				OrderData.class);
		return orderData;
	}
}
